package net.sxnic.item.mdata.action;

import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itemgjhzunit.ItemGjhzunit;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.workflow.task.Task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
public class Create extends MdataAction{
	
	private Logger logger = LoggerFactory.getLogger(Create.class);
	//设置管理单位
	private static String GLDW = "141000001";
	
	public String execute() throws Exception{
		
		try {
			//验证 每个用户只能填报1条
			
			//保存实体
			mdata.setApprover(AppContext.getUserName(request));
			mdata.setAppYear(appYear);
			mdata.setCatalog1(catalogAlias);
			mdata.setCatalog2(catalogCode);
			//默认管理单位是临汾市科技局
			mdata.setGldwCode(GLDW);
			
			mdManager.save(mdata);			
			id = mdata.getId();
			
			//设置子表数据
			//经费表
			itemFund = new ItemFund();
			itemFund.setBaseinfoId(id);
			fundManager.save(itemFund);
			//负责人表
			itemPrincipal = new ItemPrincipal();
			itemPrincipal.setBaseinfoId(id);
			principalManager.save(itemPrincipal);
			//大字段表
			itemClob =new ItemClob();
			itemClob.setBaseinfoId(id);
			clobManager.save(itemClob);
			if("009".equals(mdata.getSoperCode()))
			{
				ItemGjhzunit itemGjhzunit1= new ItemGjhzunit();
				itemGjhzunit1.setCoOrder(1);
				itemGjhzunit1.setCoType("001");
				itemGjhzunit1.setBaseinfoId(id);
				gjhzunitManager.save(itemGjhzunit1);
				ItemGjhzunit itemGjhzunit2= new ItemGjhzunit();
				itemGjhzunit2.setCoOrder(2);
				itemGjhzunit2.setCoType("001");
				itemGjhzunit2.setBaseinfoId(id);
				gjhzunitManager.save(itemGjhzunit2);
				ItemGjhzunit itemGjhzunit3= new ItemGjhzunit();
				itemGjhzunit3.setCoOrder(1);
				itemGjhzunit3.setCoType("002");
				itemGjhzunit3.setBaseinfoId(id);
				gjhzunitManager.save(itemGjhzunit3);
				ItemGjhzunit itemGjhzunit4= new ItemGjhzunit();
				itemGjhzunit4.setCoOrder(2);
				itemGjhzunit4.setCoType("002");
				itemGjhzunit4.setBaseinfoId(id);
				gjhzunitManager.save(itemGjhzunit4);
			}
			
			logger.debug("==create mdata entity success==="+mdata.getId());
			
			mdata.setStatus("正在填写");
			mdata.setStatusOrder("0100");
			mdManager.save(mdata);	
			
			//创建自己的任务
			Task task = taskManager.createSelfTaskatStartNode(id, catalogAlias, AppContext.getUserName(request));
			taskId = task.getId();
			
			//JSP对应的页面年份
			jspYear= dyManager.findJspYear(appYear, catalogAlias, catalogCode);
			
			this.addActionMessage("创建成功");
			
			//为页面显示准备
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

}
